/***************************************************************************
    begin                : aug 01 2010
    copyright            : (C) 2010 by Benoit Valot
    email                : benvalot@gmail.com
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 23 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

package val.lemonde;

import android.os.Parcel;
import android.os.Parcelable;

public class Article implements Parcelable {

	private String title;

	private String description;

	private String uri;

	private String date;

	private boolean mobileLink;

	private String media_link;

	// widget entre deux
	private boolean widgetNew;

	public Article() {
		this.title = "";
		this.description = "";
		this.uri = "";
		this.date = "";
		this.mobileLink = false;
		this.widgetNew = true;
		this.media_link = "";
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String des) {
		des = des.replaceAll("\n", "");
		des = des.replaceAll("<br />", "");
		des = des.replaceFirst("<img.*", "");
		int fin = des.length();
		if (fin > 100)
			fin = 100;
		des = des.substring(0, fin);
		des = des.replaceFirst(" \\w+$", "");
		des = des + " ...";
		this.description = new HTMLEntities().unhtmlentities(des);
	}

	public String getUri() {
		return uri;
	}

	public String getLinkUri() {
		if (this.media_link.contains(".mp3")
				|| this.media_link.contains(".mp4"))
			return (this.media_link);
		else if (this.mobileLink)
			return (uri.replace("www.lemonde.fr", "mobile.lemonde.fr"));
		else
			return (uri);
	}

	public void setUri(String uri) {
		if (uri.contains("mobile.lemonde.fr")) {
			this.mobileLink = true;
		}
		else if (uri.contains("www.lemonde.fr")) {
			// on enleve les portfolio, plus correctement gérer
			// //||uri.contains("/portfolio/")
			if (uri.contains("/article/")) {
				this.mobileLink = true;
			} else {
				this.mobileLink = false;
			}
		} else {
			this.mobileLink = false;
		}
		this.uri = uri.replaceFirst("#.*$", "");
	}

	public void setDate(String dat) {
		// <pubDate>Tue, 31 Aug 2010 19:37:08 +0200</pubDate>
		dat = dat.replaceAll("\\+0\\d+", "");
		this.date = dat.replaceFirst("^.*, ", "");
	}

	public String getDate() {
		return date;
	}

	public void setMediaLink(String string) {
		this.media_link = string;
		if (this.media_link.contains(".mp3")
				|| this.media_link.contains(".mp4"))
			this.mobileLink = true;
	}

	public boolean isMobileLink() {
		return mobileLink;
	}

	public String getMediaLink() {
		return this.media_link;
	}

	public void setWidgetNew(boolean widgetNew) {
		this.widgetNew = widgetNew;
	}

	public boolean isWidgetNew() {
		return widgetNew;
	}

	public static final Parcelable.Creator<Article> CREATOR = new Parcelable.Creator<Article>() {
		public Article createFromParcel(Parcel in) {
			return new Article(in);
		}

		public Article[] newArray(int size) {
			return new Article[size];
		}
	};

	private Article(Parcel in) {
		this.title = in.readString();
		this.description = in.readString();
		this.uri = in.readString();
		this.date = in.readString();
		this.mobileLink = in.readByte() == 1;
		this.widgetNew = in.readByte() == 1;
		this.media_link = in.readString();
//		boolean containBitmap = in.readByte() == 1;
//		//in.setDataPosition(10);
//		if(containBitmap)
//			bmImg = Bitmap.CREATOR.createFromParcel(in);
//		else 
//			Log.d("lemonde","No image for "+this.title);
	}

	@Override
	public int describeContents() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void writeToParcel(Parcel out, int arg1) {
		out.writeString(this.title);
		out.writeString(this.description);
		out.writeString(this.uri);
		out.writeString(this.date);
		out.writeByte((byte) (this.mobileLink ? 1 : 0));
		out.writeByte((byte) (this.widgetNew ? 1 : 0));
		out.writeString(this.media_link);
//		boolean containBitmap = (bmImg!=null);
//		out.writeByte((byte) (containBitmap ? 1 : 0));
//		if(containBitmap)
//			bmImg.writeToParcel(out, 0);
	}

//	public void downloadImage() {
//		//Log.d("lemonde","Image load "+ this.title);
//		if (!this.media_link.contains(".jpg"))
//			return;
//		try {
//			URL myFileUrl = new URL(this.media_link);
//			HttpURLConnection conn = (HttpURLConnection) myFileUrl
//					.openConnection();
//			conn.setDoInput(true);
//			conn.connect();
//			InputStream is = conn.getInputStream();
//			bmImg = BitmapFactory.decodeStream(is);
//			//Log.d("lemonde","Image load finish");
//			//imView.setImageBitmap(bmImg);
//		} catch (Exception e) {
//			Log.e("lemonde", e.toString());
//		}
//	}
}
